<?php
/**
 * Created by PhpStorm.
 * User: Yangjinlei
 * Date: 2018/7/6
 * Time: 10:21
 */

namespace app\index\controller;



use app\common\controller\BaseController;
use app\models\User;
use think\db\exception\ModelNotFoundException;
use think\Request;
use Workerman\Events\React\Base;

class Auth extends BaseController
{
    /**
     * @param Request $request
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\exception\DbException
     * @throws \Exception
     */
    public function login(Request $request)
    {
        $data = $this->certirs(['name','password']);
        try {
            $user = (new User())->whereOr('name', $data['name'])->whereOr('emails', $data['name'])->findOrFail()->toArray();
            $token = $this->createToken($user['id']);
            return $this->success(compact('token'));
        }catch (ModelNotFoundException $exception){
            return $this->error(405,'用户不存在');
        }
    }
}